
var USER_INFO = {
    UserID: "",
    AppID: "",
    AppKey: "",
    SecretKey: "",
    Token: "",
    UserName: "",
    RealName: "",
    KeyID: "",
    KeyCode: "",
    DistrictName: "",
    DistrictCode: "",
    OrganizeCode: "",
    OrganizeName: "",
    CredCode: "",
    UserLimitDate: "",
    Cert: "",
    Permission: [
    ]
};

$(function () {
    try {

        getTestUserInfo();
    }
    catch (e) {
        USER_INFO.Token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJQcm9qZWN0IjoiMTMyNDU0NDMyMjc3MDcwMjMzNiIsImp0aSI6InVhQjBCRTIzMjE0QTZGNzRCMCIsImlhdCI6IjE2MDQ5MDgxMDEiLCJuYmYiOiIxNjA0OTA4MTAxIiwiZXhwIjoxNjA0OTU2MTAxLCJpc3MiOiJGeXRTb2EiLCJhdWQiOlsiQ21zIiwiQ21zIl0sImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6IkFwcCJ9.snKmHLQmeLoMtW2Sxcb2c8OsZJ10A-a5HT_VgBQAodQ";
    }
});

var getUserInfo = function () {

    var vUserInfo;

    try {

        var vUserID = HttpUtil.getQueryString("op");

        if (vUserID != undefined && vUserID != null && vUserID != "") {
            //从缓存读取数据
            vUserInfo = $.cookie(vUserID);

            if (vUserInfo != undefined && vUserInfo != null && vUserInfo != "") {

                vUserInfo = JSON.parse(vUserInfo);
                USER_INFO.UserID = vUserInfo.UserID;
                USER_INFO.AppID = vUserInfo.AppID;
                USER_INFO.Token = vUserInfo.Token;
                USER_INFO.UserName = vUserInfo.UserName;
                USER_INFO.RealName = vUserInfo.RealName;
                USER_INFO.KeyID = vUserInfo.KeyID;
                USER_INFO.KeyCode = vUserInfo.KeyCode;
                USER_INFO.DistrictName = vUserInfo.DistrictName;
                USER_INFO.DistrictCode = vUserInfo.DistrictCode;
                USER_INFO.OrganizeCode = vUserInfo.OrganizeCode;
                USER_INFO.OrganizeName = vUserInfo.OrganizeName;
                USER_INFO.CredCode = vUserInfo.CredCode;
                USER_INFO.UserLimitDate = vUserInfo.UserLimitDate;
                USER_INFO.Permission = vUserInfo.Permission;


                $.cookie("SJZTUSERNAME", vUserID, { expires: 1, path: '/' });
                $.cookie(vUserID, JSON.stringify(USER_INFO), { expires: 1, path: '/' });
            }
        }
        else {
            vUserID = $.cookie("SJZTUSERNAME");

            if (vUserID != undefined) {
                USER_INFO = JSON.parse($.cookie(vUserID));
            }
            else {
                getTestUserInfo();
            }


        }
        //alert(1)
        getTestUserInfo();
    }
    catch (e) {
        alert(e)
    }
};

var getTestUserInfo = function () {

    let loginname = Cookie.getJSONCookie("loginname");

    if (loginname != undefined && loginname != null && loginname != "") {
        USER_INFO = Cookie.getJSONCookie(loginname);
    }
    else {
        var vUserName = $.cookie('userName');

        var vCode = $.cookie('qxcode');
        var vName = "";

        if (vCode != undefined) {
            if (vCode == 99999) {
                vCode = 110000;
                vName = "北京市";
            } else if (vCode == 1010) {
                vCode = 110101;
                vName = "东城区";
            } else if (vCode == 1020) {
                vCode = 110102;
                vName = "西城区";
            } else if (vCode == 1050) {
                vCode = 110105;
                vName = "朝阳区";
            } else if (vCode == 1060) {
                vCode = 110106;
                vName = "丰台区";
            } else if (vCode == 1070) {
                vCode = 110107;
                vName = "石景山区";
            } else if (vCode == 1080) {
                vCode = 110108;
                vName = "海淀区";
            } else if (vCode == 1090) {
                vCode = 110109;
                vName = "门头沟区";
            } else if (vCode == 1110) {
                vCode = 110111;
                vName = "房山区";
            } else if (vCode == 2230) {
                vCode = 110112;
                vName = "通州区";
            } else if (vCode == 2220) {
                vCode = 110113;
                vName = "顺义区";
            } else if (vCode == 2210) {
                vCode = 110114;
                vName = "昌平区";
            } else if (vCode == 2240) {
                vCode = 110115;
                vName = "大兴区";
            } else if (vCode == 2270) {
                vCode = 110116;
                vName = "怀柔区";
            } else if (vCode == 2260) {
                vCode = 110117;
                vName = "平谷区";
            } else if (vCode == 2280) {
                vCode = 110118;
                vName = "密云区";
            } else if (vCode == 2290) {
                vCode = 110119;
                vName = "延庆区";
            } else if (vCode == 2310) {
                vCode = 110140;
                vName = "经济技术开发区";
            } else if (vCode == 999999) {
                vCode = 999999;
                vName = "未知";
            } else {
                vCode = 110000;
                vName = "北京市";
            }
        }


        USER_INFO.UserID = $.cookie('uid');
        //USER_INFO.UserID = "大数据应用";
        USER_INFO.AppID = "1324544322770702336";
        USER_INFO.Token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJQcm9qZWN0IjoiMTMyNDU0NDMyMjc3MDcwMjMzNiIsImp0aSI6InVhQjBCRTIzMjE0QTZGNzRCMCIsImlhdCI6IjE2MDQ5MDgxMDEiLCJuYmYiOiIxNjA0OTA4MTAxIiwiZXhwIjoxNjA0OTU2MTAxLCJpc3MiOiJGeXRTb2EiLCJhdWQiOlsiQ21zIiwiQ21zIl0sImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6IkFwcCJ9.snKmHLQmeLoMtW2Sxcb2c8OsZJ10A-a5HT_VgBQAodQ";
        USER_INFO.UserName = vUserName;
        USER_INFO.RealName = vUserName;
        USER_INFO.KeyID = "112000100985568/051911010576";
        USER_INFO.KeyCode = "";
        USER_INFO.DistrictName = "";
        USER_INFO.DistrictCode = "";
        USER_INFO.OrganizeCode = vCode;
        USER_INFO.OrganizeName = vName;
        USER_INFO.CredCode = "499554";
        USER_INFO.UserLimitDate = "";
        USER_INFO.Permission = null;
        //     [
        //     {
        //         id: "1324545153343557632",
        //         name: "人社大数据应用",
        //         code: "DSJYY",
        //         model: [
        //             { code: "PEUI", auth: "search|edit|del|apply|approval|export" }
        //         ]
        //     }
        // ];
    }

};


var judgeMatch = function (code) {

    var vMatch = false;

    try {
        if (USER_INFO.Permission == null || USER_INFO.Permission == undefined || USER_INFO.Permission.length == 0) {

            getTokenTest();
        }

        var Permission = USER_INFO.Permission
        for (var i = 0; i < Permission.length; i++) {

            for (var j = 0; j < Permission[i].model.length; j++) {
                if (Permission[i].model[j].code == code) {
                    if (Permission[i].model[j].auth.indexOf("search") > -1) {

                        //记录日志
                        judgeLog(code, Permission[i].model[j].name);

                        return true;
                    }
                }
            }
        }
    } catch (e) { }

    return vMatch;
};


var ltip = "172.26.57.203";
var vtype = "test";//登台:test、线上:prod

var getTokenTest = function () {
    var vResult = false;
    var mycookie = $.cookie('uid');
    var qxidUrl = 'http://' + ltip + ':7001/' + vtype + '/rockapi/rsdsjpt/getqxbyuserid'
    try {
        var thisid = {
            userid: mycookie
        };
        AjaxToWebAPILogin('post', qxidUrl, thisid, 'getMes(json.data[0].qxjson);')

    } catch (e) {
    }

    return vResult;
};


// 专为登录页面
function AjaxToWebAPILogin(type, url, data, JS, IsAsync) {

    try {
        $.ajax({
            // cache: false,
            type: (type == "" ? "POST" : type),
            async: false,
            headers: getloginHeader(),
            url: url,
            data: JSON.stringify(data),
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            success: function (json) {
                //alert(JSON.stringify(json))
                eval(JS);
            },
            error: function (jqXHR) {
                alsert(jqXHR)
                // alert("发生错误：" + jqXHR.status + "\r\n请联系系统管理员！");
            }
        });
    }
    catch (e) { }
}


function getMes(mydata) {
    vPermissionResult = eval('(' + mydata + ')');
    vResult = vPermissionResult.success;
    if (vPermissionResult.success == true) {
        //USER_INFO.UserID = vPermissionResult.data.user.userID;
        USER_INFO.CredCode = vPermissionResult.data.user.userCode;
        //USER_INFO.UserName = vPermissionResult.data.user.userName;
        USER_INFO.RealName = vPermissionResult.data.user.realName;
        USER_INFO.Permission = new Array();


        //遍历data.list
        for (var i = 0; i < vPermissionResult.data.list.length; i++) {

            //子项目
            var vChildID = vPermissionResult.data.list[i].id;
            var vChildName = vPermissionResult.data.list[i].name;
            var vChildCode = vPermissionResult.data.list[i].code;
            var vChildPermission = new Array();

            //遍历主项目的children
            for (var j = 0; j < vPermissionResult.data.list[i].children.length; j++) {

                //子项目的模块
                var vModelID = vPermissionResult.data.list[i].children[j].id;
                var vModelName = vPermissionResult.data.list[i].children[j].name;
                var vModelCode = vPermissionResult.data.list[i].children[j].code;
                var vModelFuns = "";

                for (var l = 0; l < vPermissionResult.data.list[i].children[j].btnFun.length; l++) {
                    vModelFuns += vPermissionResult.data.list[i].children[j].btnFun[l].value + "|";
                }

                var vModel = {
                    id: vModelID,
                    name: vModelName,
                    code: vModelCode,
                    auth: vModelFuns
                }

                vChildPermission.push(vModel);
            }

            var vChild = {
                id: vChildID,
                name: vChildName,
                code: vChildCode,
                model: vChildPermission
            }
            USER_INFO.Permission.push(vChild);

        }
    }
}

function getloginHeader() {

    var vHeader = {};

    if (window.location.host.indexOf("localhost") > -1) {

    }
    else {
        var vAppID = 'rsdsjpt';
        var vTimestamp = getDateTimeNow();
        var vSign = hex_md5(vAppID + vTimestamp);
        vHeader = {
            appid: vAppID,
            timestamp: vTimestamp,
            sign: vSign
        }
    }

    return vHeader;
}

function getDateTimeNow() {
    var datetime = new Date();

    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
    var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
    var hour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
    var minute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
    var second = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
    return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
}

function judgeLog(code, name) {

    try {

        let url = "http://" + ltip + ":7001/jcxmglpt/api/custom/ddpt/user_log";
        if (vtype == "test") {

            url = "http://" + ltip + ":7001/testjcxmglpt/api/custom/ddpt/user_log";
        }
        let params = {
            "uid": USER_INFO.UserID,
            "realname": USER_INFO.UserName,
            "loginname": USER_INFO.UserName,
            "qxname": USER_INFO.OrganizeName,
            "qxcode": USER_INFO.OrganizeCode,
            "czsm": name,
            "zybh": code
        };

        //alert(JSON.stringify(params))

        $.ajax({
            type: "POST",
            async: false,
            headers: getloginHeader(),
            url: url,
            data: JSON.stringify(params),
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            success: function (json) {
                //alert("success:" + JSON.stringify(json))
            },
            error: function (jqXHR) {
                //alert("error:" + jqXHR)
                // alert("发生错误：" + jqXHR.status + "\r\n请联系系统管理员！");
            }
        });
    }
    catch (e) { }
}